package com.janko.springbootbase.designmode.singleton;

/**
 * @ClassName Singleton3DCL
 * @Description Double CheckLock 即：DCL也就是双重锁判断机制
 * @Author janko
 * @Date 2020/12/11 11:56
 */
public class Singleton3DCK {
    //高并发下，为了避免半初始化的状态 添加volatile
    private /*volatile*/ static Singleton3DCK singleton3DCK;

    private Singleton3DCK() {
    }
    public static Singleton3DCK newInstance() {
        if (singleton3DCK == null) {
            synchronized (Singleton3DCK.class) {
                if (singleton3DCK == null) {
                    singleton3DCK = new Singleton3DCK();
                }
            }
        }
        return singleton3DCK;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Singleton3DCK.newInstance().hashCode());
            }).start();
        }
    }
}
